﻿/*----------------------------------------------------------------
//  <copyright file="AuthController.cs" company="MicroCloud@151504200868">
//      Copyright © 2020-2024 MicroCloud Corporation, All rights reserved.
//  </copyright>
//  <site>https://gitee.com/chenmm123/microclouds</site>
//  <last-editor>cmm</last-editor>
//  <last-date>2023-11-22 15:07</last-date>
//----------------------------------------------------------------*/

namespace MicroCloud.Api.Authorization.Controllers
{
    #region "授权信息"
    /// <summary>
    /// 授权信息
    /// </summary>
    [Description("授权信息")]
    //[ModuleInfo(Code = "Auth", Name = "授权信息", OrderNo = 1)]
    public class AuthController : AuthControllerBase
    {
        #region "属性"
        #region "获取 权限服务"
        /// <summary>
        /// 获取 权限服务
        /// </summary>
        protected IAuthorityService AuthorityService => ServiceProvider.GetService<IAuthorityService>();
        #endregion

        #endregion

        #region "Action方法"
        #region "检查URL授权"
        /// <summary>
        /// 检查URL授权
        /// </summary>
        /// <param name="url">要检查的URL</param>
        /// <returns>是否有权</returns>
        [HttpGet]
        [Description("检查URL授权")]
        public virtual bool CheckUrlAuth(string url)
        {
            bool ok = this.CheckFunctionAuth(url);
            return ok;
        }
        #endregion
        #region "获取授权"
        /// <summary>
        /// 获取授权
        /// </summary>
        /// <returns>权限节点</returns>
        [HttpGet]
        [NonFunction]
        [Description("获取授权")]
        public virtual string[] GetAuthInfo()
        {
            var codes = AuthorityService.GetAuthorizedInfosByUser(User).Select(x => x.FullCode).ToArray();
            return codes;

            //IServiceProvider provider = HttpContext.RequestServices;
            //IModuleHandler moduleHandler = provider.GetRequiredService<IModuleHandler>();
            //IFunctionAuthorization functionAuthorization = provider.GetRequiredService<IFunctionAuthorization>();
            //ModuleInfo[] moduleInfos = moduleHandler.ModuleInfos;

            ////先查找出所有有权限的模块
            //List<ModuleInfo> authModules = new();
            //foreach (ModuleInfo moduleInfo in moduleInfos)
            //{
            //    bool hasAuth = moduleInfo.DependOnFunctions.All(m => functionAuthorization.Authorize(m, User).IsOk);
            //    if (moduleInfo.DependOnFunctions.Length == 0 || hasAuth)
            //    {
            //        authModules.Add(moduleInfo);
            //    }
            //}

            //List<string> codes = new();
            //foreach (ModuleInfo moduleInfo in authModules)
            //{
            //    string fullCode = moduleInfo.FullCode;
            //    //模块下边有功能，或者拥有子模块
            //    if (moduleInfo.DependOnFunctions.Length > 0
            //        || authModules.Any(m => m.FullCode.Length > fullCode.Length && m.FullCode.Contains(fullCode) && m.DependOnFunctions.Length > 0))
            //    {
            //        codes.AddIfNotExist(fullCode);
            //    }
            //}
            //return codes.ToArray();
        }
        #endregion
        #region "获取指定用户的授权-暂不开放接口"
        /// <summary>
        /// 获取指定用户的授权-暂不开放接口
        /// </summary>
        /// <returns>权限节点</returns>
        [HttpGet]
        [NonAction]
        [NonFunction]
        [Description("获取指定用户的授权")]
        public virtual string[] GetAuthInfoByUser(long userId)
        {
            var codes = AuthorityService.GetAuthorizedInfosByUser(userId.ToString()).Select(x => x.FullCode).ToArray();
            return codes;
        }
        #endregion

        #endregion

    }
    #endregion

}
